//
//  LawAccountViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/8/2.
//  Copyright © 2017年 test. All rights reserved.
//

#import "LawAccountViewController.h"

@interface LawAccountViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,JHPickerDelegate>
{
    UITextField *searchField;
    UILabel *searchType;
    UIButton *searchButton;
    NSString *searchString;
    NSInteger selectedTag;
    
    UILabel *chargeAmount;
    UILabel *refundAmount;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;



@end

@implementation LawAccountViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.barTitleLable.text = @"律所收支";
    [self initUI];
    [self getDataSource];
}
- (void)initUI{
    [self.view insertSubview:self.tableView atIndex:0];
}
- (void)getDataSource{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@company_id=%@",CHARGELISTS_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
        
        NSInteger amountNum = 0;
        self.dataSource = responseData[@"result"];
        for (NSDictionary *dic in self.dataSource) {
            NSString *amount = dic[@"sfje"];
            amountNum += [amount integerValue];
            NSLog(@"amountNum = %ld",amountNum);
            chargeAmount.text = [NSString stringWithFormat:@"%ld",amountNum];
            
        }
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@",self.dataSource);
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark - Method
- (NSInteger)compareDate:(NSString*)aDate withDate:(NSString*)bDate
{
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd"];
    NSDate *dta = [[NSDate alloc] init];
    NSDate *dtb = [[NSDate alloc] init];
    
    dta = [dateformater dateFromString:aDate];
    dtb = [dateformater dateFromString:bDate];
    NSComparisonResult result = [dta compare:dtb];
    if (result == NSOrderedAscending)
    {
        //bDate比aDate大
        return 1;
    }else if (result == NSOrderedDescending)
    {
        //bDate比aDate小
        return -1;
        
    }
    else{
        return 0;
    }
}
#pragma mark - ButtonTarget
- (void)searchAction:(UIButton *)sender{
    
    UILabel *lable1 = [self.view viewWithTag:100];
    UILabel *lable2 = [self.view viewWithTag:101];
    if ([lable1.text isEqualToString:@"请选择开始时间"]) {
        [MBProgressHUD showError:@"请选择开始时间"];
        return;
    }
    if ([lable2.text isEqualToString:@"请选择结束时间"]) {
        [MBProgressHUD showError:@"请选择结束时间"];
        return;
    }
    if ([self compareDate:lable1.text withDate:lable2.text] == -1) {
        NSLog(@"结束时间小于开始时间");
        [MBProgressHUD showError:@"结束时间小于开始时间"];
        return;
    }
    else{
      NSLog(@"查询 ");
    }
}
- (void)action_ChooseType:(UIButton *)sender{
    selectedTag = sender.tag - 100;
    JHPickView *picker = [[JHPickView alloc] initWithFrame:self.view.bounds];
    picker.delegate = self ;
    picker.selectLb.text = @"请选择";
    picker.arrayType = DeteArray;
    [self.view addSubview:picker];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 230;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LawAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LawAccountCell" forIndexPath:indexPath];
    
    cell.orderNumLable.text = [NSString stringWithFormat:@"订单号： %@",self.dataSource[indexPath.row][@"id"]];
    cell.caseNumLable.text = [NSString stringWithFormat:@"案件号： %@",self.dataSource[indexPath.row][@"ah_number"]];
    cell.chargeAmount.text = [NSString stringWithFormat:@"收费金额： %@",self.dataSource[indexPath.row][@"sfje"]];
    cell.costType.text = [NSString stringWithFormat:@"费用类型人： %@",self.dataSource[indexPath.row][@"fylx"]];
    cell.paymentDate.text = [NSString stringWithFormat:@"收支日期： %@",self.dataSource[indexPath.row][@"audit_time"]];
    cell.paymentName.text = [NSString stringWithFormat:@"收支人： %@",self.dataSource[indexPath.row][@"audit_name"]];
    cell.remark.text = [NSString stringWithFormat:@"备注： %@",self.dataSource[indexPath.row][@"bz"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    
    for (int i = 0; i < 2; i ++) {
        UIView *searchTextView = [[UIView alloc] initWithFrame:CGRectMake(5 + i * 160, 5, 150, 30)];
        searchTextView.layer.borderColor = [UIColor colorWithRed:102.0f/255.0f green:159.0f/255.0f blue:190.0f/255.0f alpha:1.0f].CGColor;
        searchTextView.layer.borderWidth = 1.0f;
        searchTextView.layer.cornerRadius = 5;
        searchTextView.layer.masksToBounds = YES;
        searchTextView.backgroundColor = [UIColor whiteColor];
        
        UIButton *searchTypeButton = [[UIButton alloc] initWithFrame:CGRectMake(5 + i * 160, 5, 150, 30)];
        searchTypeButton.tag = 200 + i;
        [searchTypeButton addTarget:self action:@selector(action_ChooseType:) forControlEvents:UIControlEventTouchUpInside];
        
        searchType = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 140, 30)];
        searchType.tag = 100 + i;
        searchType.textColor = [UIColor lightGrayColor];
        if (searchType.tag == 100) {
            searchType.text = @"请选择开始时间";
            
        }
        else if (searchType.tag == 101) {
            searchType.text = @"请选择结束时间";
        }
        searchType.font = [UIFont systemFontOfSize:13];
        
        searchButton = [[UIButton alloc] initWithFrame:CGRectMake(320, 5, 50, 30)];
        [searchButton setTitle:@"查询" forState:UIControlStateNormal];
        [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        searchButton.layer.cornerRadius = 5;
        searchButton.layer.masksToBounds = YES;
        searchButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        
        
        
        [headerView addSubview:searchTextView];
        [searchTextView addSubview:searchField];
        [headerView addSubview:searchTypeButton];
        [searchTypeButton addSubview:searchType];
    }
    
    NSArray *array = @[@"收费金额 :",@"退费金额 :"];
    for (int i = 0; i < 2; i ++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15 + i * 160, 40, 60, 30)];
        lable.text = array[i];
        lable.font = [UIFont systemFontOfSize:12];
        [headerView addSubview:lable];
        
    }
    chargeAmount = [[UILabel alloc] initWithFrame:CGRectMake(75, 40, 100, 30)];
    chargeAmount.textColor = [UIColor redColor];
    chargeAmount.font = [UIFont systemFontOfSize:13];
    
    refundAmount = [[UILabel alloc] initWithFrame:CGRectMake(235, 40, 100, 30)];
    refundAmount.font = [UIFont systemFontOfSize:13];
    
    [headerView addSubview:chargeAmount];
    [headerView addSubview:refundAmount];
    [headerView addSubview:searchButton];
    
    
    return headerView;
}

#pragma mark - JHPickerDelegate

-(void)PickerSelectorIndixString:(NSString *)str
{
    NSLog(@"%@",str);
    UILabel *lable = [self.view viewWithTag:selectedTag];
    lable.textColor = [UIColor blackColor];
    lable.text = str;
    
    
    
    
}

#pragma mark - Getter

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 180;
        [_tableView registerClass:[LawAccountCell class] forCellReuseIdentifier:@"LawAccountCell"];
        
        
    }
    return _tableView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
